package com.orangelife.mobile.myhouse.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.adapter.SimpleAdapter;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.ViewTag;
import com.orangelife.mobile.myhouse.entity.IndustryBean;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartBiz;

import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
public class CertOkRvAdapter extends SimpleAdapter<IndustryBean.House> {

    private ImageView ivSelectAll;
    private boolean isSelectAll = false;

    public CertOkRvAdapter(List<IndustryBean.House> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View itemView = LayoutInflater.from(CurryApplication.getInstance().getContext()).inflate(R.layout.item_rv_cert_ok, parent, false);
        return new CertViewHolder(itemView);
    }

    public void setImageViewSelectAll(ImageView ivSelectAll) {
        this.ivSelectAll = ivSelectAll;
        ivSelectAll.setOnClickListener(listener);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        CertViewHolder h = (CertViewHolder) holder;
        h.mText.setText(mData.get(position).getApartmentName());
        ShoppingCartBiz.checkItemHouse(mData.get(position).isSelected(), h.ivCheck);
        h.ivCheck.setTag(new ViewTag(position + ""));
        h.ivCheck.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ivCheck:
                    int pos = ((ViewTag) (view.getTag())).getIntValue();
                    isSelectAll = HouseBiz.selectOne(mData, pos);
                    ShoppingCartBiz.checkItemHouse(mData.get(pos).isSelected(), (ImageView) view);
                    ShoppingCartBiz.checkItemHouse(isSelectAll, ivSelectAll);
                    break;
                case R.id.ivSelectAll:
                    isSelectAll = HouseBiz.selectAll(mData, isSelectAll, ivSelectAll);
                    notifyDataSetChanged();
                    break;
            }
        }
    };

    private static class CertViewHolder extends RecyclerView.ViewHolder {

        TextView mText;
        ImageView ivCheck;

        public CertViewHolder(View itemView) {
            super(itemView);
            mText = (TextView) itemView.findViewById(R.id.tvApartmentID);
            ivCheck = (ImageView) itemView.findViewById(R.id.ivCheck);
        }
    }


}
